package com.liang.pcc.excel.model;

import com.alibaba.excel.annotation.ExcelProperty;
import com.liang.pcc.domain.DeviceInfo;
import com.liang.pcc.domain.PowerData;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 设备线路参数调研
 */
@Data
public class MainTransformerLineOperationData {

    @ExcelProperty(index = 0, value = "序号")
    private Long serialNumber;

    @ExcelProperty(index = 1, value = "名称配置")
    private String configName;

    @ExcelProperty(index = 2, value = "D5000名称")
    private String d5000Name;

    private Map<String, Double> timePowerMap = new LinkedHashMap<>();

    public List<PowerData> convertToPowerDataList(DeviceInfo deviceInfo, Long fileId, Long userId) {
        List<PowerData> powerDataList = new ArrayList<>();
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("H:mm");

        this.timePowerMap.forEach((timeStr, powerValue) -> {
            PowerData powerData = PowerData.builder()
                    .deviceId(deviceInfo.getId())
                    .configName(deviceInfo.getConfigName())
                    .timeSlot(LocalTime.parse(timeStr, timeFormatter))
                    .powerValue(BigDecimal.valueOf(powerValue))
                    .createdAt(new Date())
                    .fileId(fileId)
                    .userId(userId)
                    .build();
            powerDataList.add(powerData);
        });
        return powerDataList;
    }
}